const router = require('koa-router')()
const userModel = require('../model/UserModel');
const { loginPostSchema, registerPostSchema } = require('../verifys/users')
router.prefix('/users')

router.post('/login', async function (ctx, next) {
  try {
    const { error, value } = loginPostSchema.validate(ctx.request.body);
    if (error) {
      const field = error.details[0].path[0];
      let msg = '';
      switch (field) {
        case 'account':
          msg = '请输入合法的手机号';
          break;
        case 'pwd':
          msg = '密码格式不正确，请输入小写大写字母、数字、特殊字符组合的6-18位密码';
          break;
        case 'username':
          msg = '请输入合法的用户名'
          break;
        default:
          msg = '参数错误';
          break;
      }
      ctx.body = {
        code: 400,
        msg
      }
      return;
    }
    const { account, pwd } = value;
    ctx.body = await userModel.login(account, pwd)
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.post('/register', async function (ctx, next) {
  try {
    const { error, value } = registerPostSchema.validate(ctx.request.body);
    if (error) {
      const field = error.details[0].path[0];
      console.log('field', field);
      let msg = '';
      switch (field) {
        case 'account':
          msg = '请输入合法的手机号';
          break;
        case 'pwd':
          msg = '密码格式不正确，请输入小写大写字母、数字、特殊字符组合的6-18位密码';
          break;
        case 'sex':
          msg = '性别格式不正确';
          break;
        case 'photo':
          msg = '头像格式不正确';
          break;
        case 'username':
          msg = '用户名格式不正确';
          break;
        default:
          msg = '参数错误';
          break;
      }
      ctx.body = {
        code: 400,
        msg
      }
      return;
    }
    const results = await userModel.register(value)
    ctx.body = results
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.get('/getFriends', async function (ctx, next) {
  try {
    const users = await userModel.getAllUserInfo(ctx.query);
    ctx.body = users;
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
});

router.get('/logout', async function (ctx, next) {
  try {
    const res = await userModel.logout(ctx.query.id);
    ctx.body = res
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.post('/getuserlist', async (ctx, next) => {
  try {
    const res = await userModel.getUserList(ctx.request.body);
    ctx.body = res;
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.post('/editorstate', async (ctx, next) => {
  try {
    const id = ctx.request.body.id;
    const newState = ctx.request.body.state;
    const res = await userModel.editorState(id, newState);
    ctx.body = res;
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.post('/deleteuser', async (ctx, next) => {
  try {
    const id = ctx.request.body.id;
    const res = await userModel.deleteUser(id);
    ctx.body = res;
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.post('/editoruserphoto', async (ctx, next) => {
  try {
    const photo = ctx.request.body.photo;
    const id = ctx.request.body.id;
    const res = await userModel.editorUserPhoto(id, photo);
    ctx.body = res;
  } catch (e) {
    ctx.body = {
      code: 400,
      msg: e.message
    }
  }
})

router.post('/creategroup', async (ctx, next) => {
  try {
    // 创建者：{account,username,groupname,photo}
    // 参与者:[{创建者}，{其他群用户 ... account,username,photo}]
    const { creator, participants } = ctx.request.body;
    console.log(creator, participants)
    ctx.body = {
      code: 200,
      msg: '创建成功'
    }
  } catch (e) {
    return {
      code: '400',
      msg: e.message
    }
  }
})

module.exports = router
